package com.ddxz.demo.clickhouse.configuration;


import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import ru.yandex.clickhouse.BalancedClickhouseDataSource;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.concurrent.TimeUnit;

/**
 * clikckhouse配置
 *
 * @author lijun1
 * @date 2021/09/23
 */
@Configuration
@MapperScan(basePackages = "com.ddxz.demo.clickhouse.mapper", sqlSessionTemplateRef = "clickhouseSqlSessionTemplate")
public class ClickHouseConfig {
    /**
     * clickHouse操作模板，使用负载均衡clickhouses数据源
     *
     * @return
     */
    @Resource
    @Bean(name = "clickhouseDataSource")
    public DataSource clickHouseDataSource(ClickHouseProperties clickHouseProperties) {
        BalancedClickhouseDataSource balancedClickhouseDataSource
                = new BalancedClickhouseDataSource(clickHouseProperties.getUrl(), clickHouseProperties);
        balancedClickhouseDataSource.scheduleActualization(clickHouseProperties.getDelay(), TimeUnit.SECONDS);
        return balancedClickhouseDataSource;
    }
    @Bean(name = "clickhouseSqlSessionFactory")
    public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("clickhouseDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper/*.xml"));
        return sqlSessionFactoryBean.getObject();
        /*tianxu注释
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource());
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/clickhousemapper/*.xml"));
//        factoryBean.setTypeAliasesPackage("com.eversec.infrastructure.*.domain");
        return factoryBean.getObject();*/
    }

    /**
     * 创建管理器
     * @param dataSource
     * @return
     */

    @Bean(name = "clickhouseTransactionManager")
    public DataSourceTransactionManager mysqlTranscationManager(@Qualifier("clickhouseDataSource") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }


    /**
     * 创建模板
     * @param sqlSessionFactory
     * @return
     * @throws Exception
     */

    @Bean(name = "clickhouseSqlSessionTemplate")
    public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("clickhouseSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception{
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
